package com.fr.dialog;

import com.fr.base.Inter;
import java.awt.BorderLayout;
import java.awt.Dialog;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.List;
import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.BorderFactory;
import javax.swing.InputMap;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRootPane;
import javax.swing.KeyStroke;

public abstract class BaseDialog extends JDialog
{
  protected int returnValue = 1;
  protected JButton okButton;
  protected JButton cancelButton;
  protected JButton helpButton;
  protected List listeners = new ArrayList();

  protected BaseDialog(Frame paramFrame)
  {
    super(paramFrame);
  }

  protected BaseDialog(Dialog paramDialog)
  {
    super(paramDialog);
  }

  protected void initComponents()
  {
    JPanel localJPanel = (JPanel)getContentPane();
    localJPanel.setBorder(BorderFactory.createEmptyBorder(2, 4, 4, 4));
    localJPanel.setLayout(new BorderLayout(0, 4));
    applyClosingAction();
    applyEscapeAction();
    addWindowListener(new java.awt.event.WindowAdapter(this)
    {
      private final BaseDialog this$0;

      public void windowClosing()
      {
        this.this$0.doCancel();
      }
    });
  }

  public void addDialogActionListener(DialogActionListener paramDialogActionListener)
  {
    this.listeners.add(paramDialogActionListener);
  }

  public void clearDialogActionListeners()
  {
    this.listeners.clear();
  }

  protected JPanel createControlButtonPane()
  {
    JPanel localJPanel1 = new JPanel();
    localJPanel1.setLayout(new BorderLayout());
    JPanel localJPanel2 = new JPanel();
    localJPanel1.add(localJPanel2, "East");
    localJPanel2.setLayout(new GridLayout(1, 2, 6, 0));
    this.okButton = new JButton(Inter.getLocText("OK"));
    this.okButton.setMnemonic('O');
    localJPanel2.add(this.okButton);
    this.okButton.addActionListener(new ActionListener(this)
    {
      private final BaseDialog this$0;

      public void actionPerformed()
      {
        this.this$0.doOK();
      }
    });
    this.cancelButton = new JButton(Inter.getLocText("Cancel"));
    this.cancelButton.setMnemonic('C');
    localJPanel2.add(this.cancelButton);
    this.cancelButton.addActionListener(new ActionListener(this)
    {
      private final BaseDialog this$0;

      public void actionPerformed()
      {
        this.this$0.doCancel();
      }
    });
    this.helpButton = new JButton(Inter.getLocText("Help"));
    this.helpButton.setMnemonic('H');
    if (isShowHelpButton())
      localJPanel2.add(this.helpButton);
    this.helpButton.addActionListener(new ActionListener(this)
    {
      private final BaseDialog this$0;

      public void actionPerformed()
      {
        this.this$0.doHelp();
      }
    });
    getRootPane().setDefaultButton(this.okButton);
    return localJPanel1;
  }

  protected JPanel createControlButtonPane(String paramString)
  {
    JPanel localJPanel = createControlButtonPane();
    if (paramString != null)
      this.okButton.setText(paramString);
    return localJPanel;
  }

  protected JPanel createControlButtonPane(String paramString1, String paramString2)
  {
    JPanel localJPanel = createControlButtonPane();
    if (paramString1 != null)
      this.okButton.setText(paramString1);
    if (paramString2 != null)
      this.okButton.setText(paramString1);
    return localJPanel;
  }

  protected boolean isShowHelpButton()
  {
    return false;
  }

  public void doOK()
  {
    try
    {
      checkValid();
    }
    catch (Exception localException)
    {
      JOptionPane.showMessageDialog(this, localException.getMessage());
      return;
    }
    setReturnValue(0);
    dialogExit();
    int i = 0;
    int j = this.listeners.size();
    while (i < j)
    {
      DialogActionListener localDialogActionListener = (DialogActionListener)this.listeners.get(i);
      if (localDialogActionListener != null)
        localDialogActionListener.doOk();
      ++i;
    }
  }

  public void doCancel()
  {
    setReturnValue(1);
    dialogExit();
    int i = 0;
    int j = this.listeners.size();
    while (i < j)
    {
      DialogActionListener localDialogActionListener = (DialogActionListener)this.listeners.get(i);
      if (localDialogActionListener != null)
        localDialogActionListener.doCancel();
      ++i;
    }
  }

  public void doHelp()
  {
    int i = 0;
    int j = this.listeners.size();
    while (i < j)
    {
      DialogActionListener localDialogActionListener = (DialogActionListener)this.listeners.get(i);
      if (localDialogActionListener != null)
        localDialogActionListener.doHelp();
      ++i;
    }
  }

  public void checkValid()
    throws Exception
  {
  }

  private void dialogExit()
  {
    setVisible(false);
    dispose();
  }

  private void applyClosingAction()
  {
    addWindowListener(new java.awt.event.WindowAdapter(this)
    {
      private final BaseDialog this$0;

      public void windowClosing()
      {
        this.this$0.setReturnValue(1);
        BaseDialog.access$000(this.this$0);
      }
    });
  }

  protected void applyEscapeAction()
  {
    JPanel localJPanel = (JPanel)getContentPane();
    InputMap localInputMap = localJPanel.getInputMap(1);
    ActionMap localActionMap = localJPanel.getActionMap();
    localInputMap.put(KeyStroke.getKeyStroke(27, 0), "dialogExit");
    localActionMap.put("dialogExit", new AbstractAction(this)
    {
      private final BaseDialog this$0;

      public void actionPerformed()
      {
        BaseDialog.access$000(this.this$0);
      }

      public void removeDialogActionListener()
      {
        this.this$0.listeners.remove(paramDialogActionListener);
      }
    });
  }

  public int getReturnValue()
  {
    return this.returnValue;
  }

  public void setReturnValue(int paramInt)
  {
    this.returnValue = paramInt;
  }

  public Point getOKPosition()
  {
    return this.okButton.getLocationOnScreen();
  }

  public JButton getOKButton()
  {
    return this.okButton;
  }

  static void access$000(BaseDialog paramBaseDialog)
  {
    paramBaseDialog.dialogExit();
  }
}